﻿function ubb(s){
	var set=s;
	switch(set)  
   {   case "bold":
       insertUBB('<b>','</b>');
       break;   
	   case "url":
	   var p1 = prompt("显示链接的文本.\n如果为空，那么将只显示超级链接地址","");
  	   if (p1 != null) {
  		var p2 = prompt("http:// 超级链接","http://");
  		if (p2 != '' && p2 != 'http://') {
    		if (p1 != '') {
    			$('#log').parseHtml('[url='+p2+']'+p1+'[/url]');
    		}
    		else {
    			$('#log').parseHtml('[url]'+p2+'[/url]');
    		}
  		}
  	  }
	   break;
	   case "img":
	   var p = prompt('请先将图片上传到您的图库中，然后将图片地址拷下粘贴在此：', 'http://');
  	    if (p == null || $.trim(p) == '' || p.toLowerCase() == 'http://')
  		return;
		$('#log').parseHtml('[img]' + p + '[/img]');  	
	    break;
		case "video":
		var p1 = prompt("请输入视频类型.\n目前支持类型：youku、tudou、sina","");
		if (p1 != null) {
			var p2 = prompt("视频文件的地址的id","");
			if (p2 != '') {
				if (p1 != '') {
					$('#log').parseHtml('['+p1+']'+p2+'[/'+p1+']');
				}
			}
		}
		break;
	   	case "music":
		var p = prompt("Mp3音乐文件的地址","http://");
  		if (p == null || $.trim(p) == '' || p.toLowerCase() == 'http://')
  		return;
		$('#log').parseHtml('[mp3]' + p + '[/mp3]');  	
		break;
		case "pass":
		insertUBB('[pass=]','[/pass]');
	    break;	
   }	
}	
var insertUBB = function (html,end) {   	 
	if(end =='')
	{$('#log').parseHtml(html);}
	else{
		var val = $('#log').selection(); 
  	if (val == '') {
		if (end=='[/pass]'){
			alert('请选加密内容');
			}
		else{			
  		alert('请选择文字');}
  		return false;
  	}
  	else {		
  		$('#log').parseHtml(html + val + end);  		
  	} 	}
}
$.fn.extend({
	selection: function() {
		var txt = '';
		var doc = this.get(0).document;
		if (doc) {
			var sel = doc.selection.createRange();
				if (sel.text.length > 0)
					txt = sel.text;
		}
		else if (this.get(0).selectionStart || this.get(0).selectionStart == '0') {
			var s = this.get(0).selectionStart;
			var e = this.get(0).selectionEnd;
			if (s != e) {
				txt = this.get(0).value.substring(s, e);
			}
		}
		return $.trim(txt);
	},
	parseHtml: function(t) {
		var doc = this.get(0).document;
		if (doc) {
			this.get(0).focus();
			doc.selection.createRange().collapse;			
			this.get(0).document.selection.createRange().text = t;
		}
		else if (this.get(0).selectionStart || this.get(0).selectionStart == '0') {
			var s = this.get(0).selectionStart;
			var e = this.get(0).selectionEnd;
			var val = this.get(0).value;
			var start = val.substring(0,s);
			var end = val.substring(e);
			this.get(0).value = start + t + end;
		} 
	}
})
function ckradd(e,f){
  if($("#"+e+"pname").val()==""){
  alert("请输入昵称后再发表！");
  $("#"+e+"pname").focus();
  return false;
  }
  var val=$("#"+e+"plog").val();
  if(val.length<5 || val.length>130){
	alert("一条留言最多130个字哦，亲~！");
   $("#"+e+"plog").focus();
   return false;  	  
  }
  var code=$("#safecode").val();
  if (f=='1'&&code==''){	
      alert("请正确输入右侧答案！");$("#safecode").focus();return false; 
 } 
}

function ckse(){
	var val=$("#key").val();
  if(val.length<2 || val.length>10){
	alert("关键词必须在2-10字之间，请修改后再提交！");
   $("#key").focus();
   return false;  	  
  }
}
function StopButton(id,s){
	$("#"+id).attr("disabled",true);　
	$("#"+id).val("发表("+s+")");
	if(--s>0){
		 setTimeout("StopButton('"+id+"',"+s+")",1000);
	}
	if(s<=0){
		$("#"+id).val(' 发 表 ');
	    $("#"+id).removeAttr("disabled");
	} 
}
function addlog(){	
  if($("#log").val()==""){
    alert("请输入内容后再发表！");
    $("#log").focus();
    return false;
  }
	var nlog = $("#log").val(),npic = $("#pic").val();
	$.post("./class/ajax.asp?do=addsave",{logs:nlog,pic:npic},function(txt){tb_remove();location.href="./";});	
}

function editlog(did){	
  if($("#log").val()==""){
    alert("请输入内容后再发表！");
    $("#log").focus();
    return false;
  }
	var nlog = $("#log").val(),npic = $("#pic").val(),nopic = $("#opic").val();
	$.post("./class/ajax.asp?do=editsave&id="+did,{logs:nlog,pic:npic,opic:nopic},function(txt){tb_remove();$("#log-"+did+" .textPost").html(txt);});	
}

function del(id,v){
	if(confirm('确定要删除吗?'))
	{	
		$.get("./class/ajax.asp?do=dellog&id="+id,function(s){if(s=='ok'){ if(v=='1'){location.href="./";}else{$("#log-"+id).fadeOut();} }else{alert(s);}});
     }
}
function delpl(id,pid){
	if(confirm('确定要删除吗?'))
	{	
		$.get("./class/ajax.asp?do=delpl&id="+id+"&pid="+pid,function(s){if(s=='ok'){$("#Com-"+pid).fadeOut();}else{alert(s);}});
     }
}
function delpic(url){
	if(url==''){
		alert('没有图片可以删除！');
		return false;
	}
	if(confirm('确定要删除吗?重新上传将自动删除旧图片'))
	{	
		$.get("./class/ajax.asp?do=delpic&pic="+url,function(s){if(s=='ok'){$("#pic").val('');$("#delpic").fadeOut();$('#msg').text('删除成功')}else{alert(s);}});
     }
}
function shpl(id){
		$.get("./class/ajax.asp?do=shpl&pid="+id,function(s){if(s=='ok'){$("#sh-"+id).fadeOut();}else{alert(s);}});
}
function zd(id){
	var zdobj=$("#zd-"+id);
	var xval=0;
	if(zdobj.text()=='置顶'){xval=1};
	$.get("./class/ajax.asp?do=zd&id="+id+"&x="+xval,function(s){if(s.substr(0,3)=='ok:'){zdobj.text(s.substr(3, s.length));}else{alert(s);}});
}
function addpl(id){	
	var npname = $("#pname").val(),nplog = $("#plog").val(),nscode=$("#safecode").val();	
	$.post("./class/ajax.asp?do=addpl&id="+id, {pname:npname, plog:nplog,scode:nscode}, function(txt) {
	 if(txt.indexOf('Erro')==0){alert(txt);$("#safecode").val('');reloadcode();$("#safecode").focus();}else
	 {$("#comment_list").append(txt);$("#plog").val('');$("#safecode").val('');reloadcode();StopButton('add',10);}											 
	});		
}
function plsave(id,pid,x){	
	var npname = $("#epname").val(),nplog = $("#eplog").val(),nrlog = $("#erlog").val();
	$.post("./class/ajax.asp?do=plsave&id="+id+"&pid="+pid+"&x="+x, { pname:npname, plog:nplog,rlog:nrlog},function(txt){tb_remove();$("#Ctext-"+pid).html(txt);});	
}
function links(id){
	var nlinks = $("#links").val();
	if(nlinks==''){
		alert('请输入内容后再发表');
		return false;
	}
	$.post("./class/ajax.asp?do=links&id="+id, {links:nlinks},function(txt){tb_remove();$('#link'+id).html(txt);});
}
function ckpass(id){	
	var pss= $("#password"+id).val();
	if (pss!=''){
	$.post("./class/ajax.asp?do=ckpass&id="+id, {ps:pss}, function(txt) {if(txt=="Erro"){alert('密码不正确');$("#password"+id).val('');}else{$("#pass_content"+id).html(txt)}});}	
}
function DotRoll(elm) {
    $("body,html").animate({ scrollTop: $("a[name='" + elm + "']").offset().top }, 500);
}
function reloadcode(){$('#codeimg').attr('src','./class/codes.asp?n='+Math.random());}
function upic(){
new AjaxUpload('upic', {
            action: './class/upload.asp',
			name: 'imgFile',
            responseType: 'json',
			onSubmit : function(file,ext){
				if (ext && /^(jpg|png|jpeg|gif)$/.test(ext)){
					this.setData();					
					$('#msg').text('正在上传文件：' + file);	
				} else {
					$('#msg').text('图片格式不正确');
					return false;				
				}		
			},
			onComplete : function(file,response){				
				if(response.error=="1"){$('#msg').text(response.message)}else{
					this.disable();
					$('#msg').text("上传成功");
					if ($('#log').val()==''){
					$('#log').val('分享图片');
					}
					$("#pic").val(response.url);
					}
								
			}		
});
}